﻿using CustomPlayerEffects;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using System.ComponentModel;
using UnityEngine;
using Utils;


namespace RoundEvent.Event
{
    public class FogEvent
    {
        string[] commands = new string[] { "event", "eve" };

        [PluginEvent(ServerEventType.RemoteAdminCommandExecuted)]
        public void OnRemoteCommandRun(RemoteAdminCommandExecutedEvent ev)
        {
            string command = ev.Command.ToLower();

            if (!commands.Contains(command)) return;

            if (ev.Arguments[0] != "fog") return;

            if (ev.Arguments.Length == 1)
            {
                this.DoNoticeFog();
                return;
            }


            if (ev.Arguments.Length == 2 && ev.Arguments[1] == "done")
            {
                this.DoNoticeFogDone();
                return;
            }
        }



        [PluginEvent(ServerEventType.LczDecontaminationStart)]
        public void OnLczStart(LczDecontaminationStartEvent ev)
        {
            bool isLucky = UnityEngine.Random.Range(0, 1f) <= 0.1f;

            if (isLucky)
            {
                this.DoNoticeFog();
            }
        }



        [Description("")] Coroutine m_HappyCOR;

        private void DoNoticeFog()
        {
            Cassie.Message("<color=green>[气象监测]</color> 大雾<color=yellow>黄色预警</color>", false, false, true);

            m_HappyCOR = TimeCoroutine.Loop(
                360,
                1,
                () => Player.GetPlayers().FindAll(e => e.IsAlive).ForEach(e => e.ReferenceHub.playerEffectsController.ChangeState<FogControl>(4, 2, false)),
                () => DoNoticeFogDone());
        }
        private void DoNoticeFogDone()
        {
            TimeCoroutine.Stop(m_HappyCOR);

            Player.GetPlayers()
                .FindAll(e => e.IsAlive)
                .ForEach(e => e.ReferenceHub.playerEffectsController.ChangeState<FogControl>(1, 1, false));


            Cassie.Message("<color=green>[气象监测]</color> 换气系统已启动！<color=yellow>预警解除</color>", false, false, true);
        }
    }
}
